package com.vcredit.creditcat.service.product.sd;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.vcredit.creditcat.manager.SMSMessageManager;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.model.CustAccount;
import com.vcredit.creditcat.model.CustProductApply;
import com.vcredit.creditcat.service.CustAccountService;
import com.vcredit.creditcat.service.product.IProductApplyResultService;
import com.vcredit.creditcat.service.product.shared.ProductApplyServiceAdaptor;
import com.vcredit.creditcat.vo.product.sd.SDQueryParamVo;
import com.vcredit.creditcat.vo.product.sd.SDQueryResultVo;
import com.vcredit.creditcat.vo.product.sd.SDResultVo;
import com.vcredit.framework.util.DateUtil;
import com.vcredit.framework.util.HttpRequest;
import com.vcredit.framework.util.JacksonUtil;
import com.vcredit.creditcat.enumtype.SmsDescEnum;

@Service(ProductApplyServiceAdaptor.RESULT_BEANNAME_PREFIX+3)
public class SDApplyResultServiceImpl implements IProductApplyResultService {
	private Logger logger = LoggerFactory.getLogger(SDApplyResultServiceImpl.class);


	@Value("${product.sd.queryUrl}")
	private String queryUrl;	
	@Value("${product.sd.creditUrl}")
	private String creditUrl;	
	private static final Integer TIMEOUT = 60; // 请求超时时间60s	
	@Autowired
	private CustAccountService custAccountService;	
	@Autowired
	public SMSMessageManager sMSMessageManager;
	
	@Override
	public CustProductApply getProductApplyResult(Cust cust, long productId) throws Exception {
			
		SDQueryParamVo queryParam = new SDQueryParamVo(cust.getCustIdno(), cust.getCustName(),cust.getCustMobile());
		String queryParamStr = JacksonUtil.Obj2Json(queryParam);
		logger.info("闪贷查询状态入参："+queryParamStr);
		String queryResultStr = HttpRequest.doJson(queryUrl, queryParamStr, TIMEOUT);
		logger.info("闪贷查询状态出参："+queryResultStr);
		SDResultVo<SDQueryResultVo> result = JacksonUtil.Json2Obj(queryResultStr, new TypeReference<SDResultVo<SDQueryResultVo>>(){});
		CustProductApply custProductApply = new CustProductApply();
		if("1".equals(result.getStatus())) {
			if(result.getData()!=null){							
				SDQueryResultVo content = result.getData();
				int state = content.getOrderState();
				switch(state){
					case 7 :
						custProductApply.setAuditResult(1);
						custProductApply.setIsApply(false);
						custProductApply.setRejectMsg("闪贷授信成功");
						custProductApply.setCreditAmt(content.getCreditAmt() == null ? 0 : content.getCreditAmt());
						if(content.getCreditExpiretime()!=null){
							long time = content.getCreditExpiretime();
							SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
							String creditExpiredTime = format.format(time);
							custProductApply.setCreditExpiretime(creditExpiredTime);
						}
						custProductApply.setServiceRate(content.getServiceRate());					
						custProductApply.setFormalitiesRate(content.getFormalitiesRate());
						custProductApply.setInterestRateMonthly(content.getInterestRateMonthly());
						try { //发送成功短信，不影响主流程
							CustAccount account = new CustAccount();
							account.setCustId(cust.getCustId());
							CustAccount custAccount = custAccountService.find(account).get(0);
							String mobile = custAccount.getUserName();
							String appdesc = custAccount.getAppDesc();	
							String userName = custAccount.getAccountRealname();
							appdesc=appdesc.split(";")[0].toString();
							String name=SmsDescEnum.getNameByAppDesc(appdesc);
							sMSMessageManager.sendCreditSuccess(mobile, name, userName);
						} catch (Exception e) {
							
						}
						return custProductApply;
					case 8 :
						custProductApply.setAuditResult(0);
						if(StringUtils.isEmpty(content.getRemark())){
							custProductApply.setRejectMsg("闪贷授信拒绝");
						}else{
							custProductApply.setRejectMsg("闪贷授信拒绝："+ content.getRemark());
						}	
						custProductApply.setIsApply(true);
						custProductApply.setCreditApplyTime(DateUtil.formatDateTime(DateUtil.addDayTime(new Date(),30)));
						try {  //发送失败短信，不影响主流程
							CustAccount account = new CustAccount();
							account.setCustId(cust.getCustId());
							CustAccount custAccount = custAccountService.find(account).get(0);
							String mobile = custAccount.getUserName();
							String appdesc = custAccount.getAppDesc();	
							String userName = custAccount.getAccountRealname();
							appdesc=appdesc.split(";")[0].toString();
							String name=SmsDescEnum.getNameByAppDesc(appdesc);
							sMSMessageManager.sendCreditFail(mobile, name, userName);
						} catch (Exception e) {
							
						}
						return custProductApply;
					case 9 :
						custProductApply.setAuditResult(7);
						custProductApply.setRejectMsg("闪贷信用补救");
						custProductApply.setIsApply(false);
						return custProductApply;
				}
			}			
		}
		return custProductApply;
	}
}
